package pab.ui;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JComboBox;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import pab.core.service.ExchangeRate;
import pab.core.service.FixedExchangeRate;
import pab.core.transaction.Currency;
import pab.core.transaction.ProjectedInterval;

public class GeneralAuxiliary {
	public static void loadCurrency(JComboBox cboCurrency){
		ExchangeRate er = new FixedExchangeRate();;
		for(Currency c : er.getSupportedCurrencies()){
			cboCurrency.addItem(c.getCode());
		}
	}
	
	public static Currency getCurrencyByCode(String code){
		ExchangeRate er = new FixedExchangeRate();;
		for(Currency c : er.getSupportedCurrencies()){
			if(c.getCode().equals(code))
				return c;
		}
		return null;
	}
	
	public static XMLGregorianCalendar long2Gregorian(long date) {
		DatatypeFactory dataTypeFactory;
		try {
		dataTypeFactory = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
		throw new RuntimeException(e);
		}
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTimeInMillis(date);
		return dataTypeFactory.newXMLGregorianCalendar(gc);
		}
	public static XMLGregorianCalendar date2Gregorian(Date date) {
		return long2Gregorian(date.getTime());
		}
	
	public static XMLGregorianCalendar DateToXMLGregorianCalendar(Date date){
		GregorianCalendar c = new GregorianCalendar();
		c.setTime(date);
		try {
			return DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static Date XMLGregorianCalendarToDate(XMLGregorianCalendar xmlGregorianCalendar){
		return xmlGregorianCalendar.toGregorianCalendar().getTime();
	}
	
	public static ProjectedInterval getIntervalByName(String interval){
		
		if(interval.equals("Month"))
			return ProjectedInterval.MONTH;
		if(interval.equals("Quarter"))
			return ProjectedInterval.QUARTER;
		if(interval.equals("6-Month"))
			return ProjectedInterval.HALF_YEAR;
		
		return ProjectedInterval.YEAR;
	}
	
	public static String getIntervalCanonicalName(ProjectedInterval interval){
		if(interval == ProjectedInterval.MONTH)
			return "Month";
		
		if(interval == ProjectedInterval.MONTH)
			return "Month";
		
		if(interval == ProjectedInterval.MONTH)
			return "Month";
		
		return "Year";
	}
	public static String toLink(String s){
		if (s.isEmpty())
			return "";
		return "<html><font color=\"#0000CF\"><u>"+s+"</u></font></html>";
	}
	
	public static String removeLink(String s){
		if (s.isEmpty())
			return "";
		else
			return s.replaceAll("<html><font color=\"#0000CF\"><u>", "").replaceAll("</u></font></html>", "");
	}
	
	public static double round(double d, int decimalPlace){

	    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
	    BigDecimal bd = new BigDecimal(Double.toString(d));
	    bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
	    return bd.doubleValue();
	}
	
	public static String formatCurrency(double amount){

		NumberFormat formatter = NumberFormat.getCurrencyInstance();
		return formatter.format(amount);
	}
}
